package com.situ.mall.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.situ.mall.mapper.CartMapper;
import com.situ.mall.mapper.OrderItemMapper;
import com.situ.mall.pojo.entity.Order;
import com.situ.mall.pojo.entity.OrderItem;
import com.situ.mall.pojo.vo.CartListVO;
import com.situ.mall.pojo.vo.OrderListVO;
import com.situ.mall.util.JSONResult;
import com.situ.mall.util.OrderDateNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.mapper.OrderMapper;
import com.situ.mall.mapper.ShippingMapper;
import com.situ.mall.pojo.entity.Shipping;
import com.situ.mall.service.IOrderService;

@Service
public class OrderServiceImpl implements IOrderService{
	@Autowired
	private ShippingMapper shippingMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private CartMapper cartMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;

	@Override
	public List<Shipping> selectByUserId(Integer id) {
		List<Shipping> list = shippingMapper.selectByUserId(id);
		return list;
	}

	@Override
	public JSONResult addToOrder(Order order) {
		// order中已有userId、shippingId
		order.setStatus(10);
		order.setPostage(0);
		order.setPaymentType(1);// 支付方式
		order.setOrderNo(OrderDateNoUtil.getOrderNo());
		// order中已有userId、shippingId、订单状态、运费、订单号、支付方式
		BigDecimal payment = BigDecimal.valueOf(0.0);
		List<CartListVO> list = cartMapper.selectCheckByUser(order.getUserId());
		for (CartListVO cartVO : list) {
			System.out.println(cartVO);
			OrderItem orderItem = new OrderItem();
			orderItem.setUserId(cartVO.getUserId());
			orderItem.setOrderNo(order.getOrderNo());
			orderItem.setProductId(cartVO.getProductId());
			orderItem.setProductName(cartVO.getProductName());
			orderItem.setProductImage(cartVO.getProductImage());
			orderItem.setCurrentUnitPrice(cartVO.getProductPrice());
			orderItem.setQuantity(cartVO.getQuantity());
			// 该条商品总价
			BigDecimal quantity = BigDecimal.valueOf(cartVO.getQuantity());
			BigDecimal totalPrice = quantity.multiply(cartVO.getProductPrice());
			orderItem.setTotalPrice(totalPrice);
			orderItemMapper.insert(orderItem);

			// 订单总共支付
			payment = payment.add(totalPrice);
		}
		int deleteCart = cartMapper.deleteByUserCheck(order.getUserId());
		order.setPayment(payment);
		System.out.println(order);
		int count = orderMapper.insert(order);
		return count + deleteCart == 1 + list.size() ? JSONResult.success("成功生成订单", order.getOrderNo()) : JSONResult.error("生成订单失败");
	}
 
	@Override
	public OrderListVO selectByOrderNo(Long orderNo) {
		Order order = orderMapper.selectByOrderNo(orderNo);
		List<OrderItem> orderItem = orderItemMapper.selectByOrderNo(orderNo);
		Shipping shipping = shippingMapper.selectByPrimaryKey(order.getShippingId());
		System.out.println(order);
		System.out.println(orderItem);
		System.out.println(shipping);
		OrderListVO orderListVO = new OrderListVO();
		orderListVO.setOrder(order);
		orderListVO.setOrderItem(orderItem);
		orderListVO.setShipping(shipping);
		System.out.println(orderListVO);
		return orderListVO;
	}

	@Override
	public JSONResult deleteByOrderNo(Long orderNo) {
		int count = orderMapper.deleteByOrderNo(orderNo);
		count += orderItemMapper.deleteByOrderNo(orderNo);
		return count >= 2 ? JSONResult.success("取消成功") : JSONResult.error("取消失败");
	}


}
